//
//  XRRootViewController.m
//  X-Touch2
//
//  作用：统一调度全屏界面view的显示，不允许无限地叠加。
//       除了弹出窗口以外，其它view的移近之前必须移除上一个view
//
//  Created by shengchao yang on 12-3-8.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#define animateTime 0.3f   //动画切换时间间隔

#import "XRRootViewController.h"

#import "MenuBookChoose.h"
#import "GuestOrderingView.h"
#import "FullScreenMode.h"
#import "MessageBoxBaseView.h"
#import "CookSelectView.h"
#import "MyOrderMenuManagement.h"
#import "RemoteMenuDetail.h"
#import "RemoteMenuDetail1.h"
#import "Imports1.h"

#import "PluPackageManagement.h"

@implementation XRRootViewController
@synthesize currentController;
@synthesize currentController1;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
#pragma mark 应用逻辑
-(void) ViewAnimationStoped{
    MyNSLog(@"in ViewAnimationStoped method");
	while ([self.view viewWithTag:TagOfUIStepPreRemoveView]) {
		[[self.view viewWithTag:TagOfUIStepPreRemoveView] removeFromSuperview];
	}
    AppSession.isAnimating = AppSession.isPopOutFullScreen = NO;
}
//切换全屏的UIViewController
-(void) ControllerChange:(NSNotification *)note{
	NSDictionary *infos = [note userInfo];
	if ([infos objectForKey:@"FromNibName"] && [infos objectForKey:@"ToNibName"]) {
        NSString *fromNib = (NSString *)[infos objectForKey:@"FromNibName"];
		NSString *toNib = (NSString *)[infos objectForKey:@"ToNibName"];
        MyNSLog(@"fromNib=%@,toNib=%@", fromNib,toNib);
        
        //根据页面的切换，判断滑动的方向
        if ([fromNib isContainString:@"OEMView"] && [toNib isContainString:@"MenuBookChoose"]) {
            AppSession.isViewRemoveFromLeft = YES;
        }else if([fromNib isContainString:@"Start"] && [toNib isContainString:@"MenuBookChoose"]){
            AppSession.isViewRemoveFromLeft = YES;
        }else if([fromNib isContainString:@"GuestOrderingView"] && [toNib isContainString:@"MyOrderView"]){
            AppSession.isViewRemoveFromLeft = YES;
        }else if([fromNib isContainString:@"MyOrderView"] && [toNib isContainString:@"GuestOrderingView"]){
            AppSession.isViewRemoveFromLeft = NO;
        }else if([fromNib isContainString:@"MenuBookChoose"]){
            AppSession.isViewRemoveFromLeft = YES;
        }else if([toNib isContainString:@"MenuBookChoose"]){
            AppSession.isViewRemoveFromLeft = NO;
        }else if([toNib isContainString:@"OEMView"]){
            AppSession.isViewRemoveFromLeft = NO;
        }
        
        //完成全屏页面切换直接的动画效果(用第三方库)
        if ([self.view viewWithTag:TagOfUIStepView]) {
            [[self.view viewWithTag:TagOfUIStepView] setTag:TagOfUIStepPreRemoveView];
            
            if (AppSession.isViewRemoveFromLeft) {
                [[self.view viewWithTag:TagOfUIStepPreRemoveView] slideOutTo:kFTAnimationLeft duration:animateTime delegate:self startSelector:nil stopSelector:@selector(ViewAnimationStoped)];
            }
            else{
                [[self.view viewWithTag:TagOfUIStepPreRemoveView] slideOutTo:kFTAnimationRight duration:animateTime delegate:self startSelector:nil stopSelector:@selector(ViewAnimationStoped)];
            }
        }
        
        //动态创建ViewController对象
        UIViewController *viewController = [[NSClassFromString(toNib) alloc] initWithNibName:toNib bundle:nil];
        viewController.view.tag = TagOfUIStepView;
        if ([self.view viewWithTag:TagOfUIStepPreRemoveView]) {
            [self.view insertSubview:viewController.view belowSubview:[self.view viewWithTag:TagOfUIStepPreRemoveView]];
        }else{
            [self.view addSubview:viewController.view];
        }
        self.currentController = viewController;//这里隐藏调用上一个[controller dealloc]
        [viewController release];//临时变量要release
        
        //oem出现的时候有fallIn动画
        if ([toNib isContainString:@"OEMView"]) {
            [[self.view viewWithTag:TagOfUIStepView] fallIn:animateTime delegate:nil];
        }
	}
}
//弹出全屏浏览窗口
-(void) PopOutPluPlu{
    FullScreenMode *xrmbsc = [[FullScreenMode alloc] initWithNibName:@"FullScreenMode" bundle:nil];
    MessageBoxBaseView *mbbv = [[MessageBoxBaseView alloc] initWithContentViewController:xrmbsc];
    [self.view addSubview:mbbv];
    [mbbv release];
}
//弹出烧法选择窗口
-(void) PopOutSelectCookWithInfo:(NSNotification *)note{
    NSDictionary *infos = [note userInfo];
    MyOrderDishes * pluObj = (MyOrderDishes *)[infos objectForKey:@"pluObj"];
    CookSelectView *xrmbsc = [[CookSelectView alloc] initWithNibName:@"CookSelectView" withObj:pluObj];
    MessageBoxBaseView *mbbv = [[MessageBoxBaseView alloc] initWithContentViewController:xrmbsc];
    [self.view addSubview:mbbv];
    [mbbv release];
}
//弹出正在下载小窗口
-(void) ShowMyProcessView:(NSNotification *)note{
    NSDictionary *infos = [note userInfo];
    NSString * message = (NSString *)[infos objectForKey:@"message"];
    _myProcessView = [[MyProcessView alloc] initWithNibName:@"MyProcessView" withMessage:message];
    MessageBoxBaseView *mbbv = [[MessageBoxBaseView alloc] initWithContentViewController:_myProcessView];
    [self.view addSubview:mbbv];
    [mbbv release];
    
    [_myProcessView startProcessing];
}
-(void) CloseMyProcessView{
    [_myProcessView closeMyProcessView];
}
//弹出本地主单管理界面
-(void) PopOutMyOrderMenuManagement:(NSNotification *)note{
    if (AppSession.popController.popoverVisible) {//退出PopoverViewController
        [AppSession.popController dismissPopoverAnimated:NO];
    }
    
    NSDictionary *infos = [note userInfo];
    NSString * obj = (NSString *)[infos objectForKey:@"obj"];
    MyOrderMenuManagement *xrmbsc = [[MyOrderMenuManagement alloc] initWithNibName:@"MyOrderMenuManagement" withObj:obj];
    MessageBoxBaseView *mbbv = [[MessageBoxBaseView alloc] initWithContentViewController:xrmbsc];
    [self.view addSubview:mbbv];
    [mbbv release];
}
-(void) PopOutRemoteMenuDetail{//弹出远程主单明细
    //根据menu号查询明细    
    RemoteMenuDetail1 *rmd1 = [[RemoteMenuDetail1 alloc] initWithNibName:@"RemoteMenuDetail1" bundle:nil];
    [self.view addSubview:rmd1.view];
    self.currentController1 = rmd1;
    [rmd1 release];
}
 
-(void) AlertErrMsg{
    NSString *errmsg = TranslateStr(@"上一次送单有误");
    AppSession.isAnimating = NO;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TranslateStr(@"提示") 
                                                    message:errmsg
                                                   delegate:nil 
                                          cancelButtonTitle:TranslateStr(@"确定") 
                                          otherButtonTitles:nil ];
    [alert show];
    [alert release];
}
//弹出套餐管理窗口
-(void) PopOutPluPackage{
    PluPackageManagement *ppm = [[PluPackageManagement alloc] initWithNibName:@"PluPackageManagement" bundle:nil];
    [self.view addSubview:ppm.view];
    self.currentController1 = ppm;
    [ppm release];
}

#pragma mark - View lifecycle
- (void)viewDidLoad{     
    ////////////////////初始化数据 begin////////////////////
    [AppSession initPluBook];//初始化菜本
    [AppSession initSpecialSorts];//初始化烧法类别
    ////////////////////初始化数据 end//////////////////////
    
    addNObserver(@selector(ControllerChange:), SysMsg_RootView_ControllerChange);
    addNObserver(@selector(PopOutPluPlu), SysMsg_RootView_PopOutPluPlu);
    addNObserver(@selector(PopOutSelectCookWithInfo:), @"SysMsg_RootView_PopOutSelectCookWithInfo");
    addNObserver(@selector(ShowMyProcessView:), @"SysMsg_RootView_ShowMyProcessView");
    addNObserver(@selector(CloseMyProcessView), @"SysMsg_RootView_CloseMyProcessView");
    addNObserver(@selector(PopOutMyOrderMenuManagement:), @"SysMsg_RootView_PopOutMyOrderMenuManagement");
    addNObserver(@selector(PopOutRemoteMenuDetail), @"SysMsg_RootView_PopOutRemoteMenuDetail");
    addNObserver(@selector(AlertErrMsg), @"SysMsg_RootView_AlertErrMsg");
    addNObserver(@selector(PopOutPluPackage), @"SysMsg_RootView_PopOutPluPackage");
    
    NSString *startViewController = @"OEM2ViewController";
    if (!AppSession.isOEMDisplay) {
        startViewController = @"MenuBookChoose";
    }
    NSDictionary *infos = [NSDictionary dictionaryWithObjectsAndKeys:@"Start", @"FromNibName", startViewController,@"ToNibName",nil];
    postNWithInfos(SysMsg_RootView_ControllerChange, self, infos);
    
    [super viewDidLoad];
}
-(void)dealloc{
    removeNObserver(SysMsg_RootView_ControllerChange);
    removeNObserver(SysMsg_RootView_PopOutPluPlu);
    removeNObserver(@"SysMsg_RootView_PopOutSelectCookWithInfo");
    removeNObserver(@"SysMsg_RootView_ShowMyProcessView");
    removeNObserver(@"SysMsg_RootView_CloseMyProcessView");
    removeNObserver(@"SysMsg_RootView_PopOutMyOrderMenuManagement");
    removeNObserver(@"SysMsg_RootView_PopOutRemoteMenuDetail");
    removeNObserver(@"SysMsg_RootView_AlertErrMsg");
    removeNObserver(@"SysMsg_RootView_PopOutPluPackage");
    [currentController release];
    [currentController1 release];
    [super dealloc];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
- (void)viewDidUnload{
    [self setCurrentController:nil];
    [self setCurrentController1:nil];
    [super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ;
}

@end
